//
//  JDOrderDetailCell.swift
//  DragonWallet
//
//  Created by volantPig on 2024/8/20.
//

import UIKit
import SwiftyJSON

class JDOrderDetailCell: UITableViewCell {

    var orderType: JDOrderType = .waitingPay
    /// 物流信息
    var logisticsInfoCallBack: ((String)->())?
    
    var goodsCount: Int = 0
    var containerViewOne: UIView?
    var storeNameLabel: UILabel?
    var tagLabel: UILabel?
    var goodsInfoTableView: UITableView?
//    var goodsInfoView: JDOrderGoodsView?
    
    var containerViewTwo: UIView?
    /// 关税金额
    var guanshuiPrice: JDCellInfoCellSubView?
    /// 境内物流费
    var jingneiwuliuPrice: JDCellInfoCellSubView?
    /// 跨境物流费
    var kuajingwuliuPrice: JDCellInfoCellSubView?
    
    var containerViewThree: UIView?
    /// 跨境订单号
    var orderId: JDCellInfoCellSubView?
    /// 海关通关单号
    var haiguanID: JDCellInfoCellSubView?
    /// 供应商编号
    var gongyingshangNum: JDCellInfoCellSubView?
    
    var containerViewThreeExtra: UIView?
    /// 下单时间
    var orderCreateTime: JDCellInfoCellSubView?
    /// 订单支付时间
    var orderPayTime: JDCellInfoCellSubView?
    /// 配送/发货时间
    var deliveryOrderTime: JDCellInfoCellSubView?
    /// 到货时间
    var arriveTime: JDCellInfoCellSubView?
    /// 收货时间
    var finishTime: JDCellInfoCellSubView?
    
    var containerViewFour: UIView?
    var phoneTitleLabel: UILabel?
    var callButton: UIButton?
    
    var containerViewFive: UIView?
    var expressTitle: UILabel?
    var expressId: UILabel?
    var expressDeatilButton: UIButton?
    
    var inputContainerViewTitle: UILabel?
    var inputContainerView: UIView?
    var shopperNameInputView: UITextField?
    /// 购买人真实姓名
    var shopperRealNameLabel: UILabel?
    var shopperIDNumberInputView: UITextField?
    /// 
    var shopperIDNumberLabel: UILabel?
    var shopperMessageInputView: UITextView?
    
    var addressInfoViewTitle: UILabel?
    var addressInfoImgV: UIImageView?
    var addressInfoContainerView: UIView?
    var shopperNameLabel: UILabel?
    var shopperTelLabel: UILabel?
    var shopperAddressLabel: UILabel?
    var changeAddressButton: UIButton?
    
    var tipsViewTitle: UILabel?
    var hintImgV: UIImageView?
    var tipsContainerView: UIView?
    var tipsLabel: UILabel?
    
    var shopName: String?
    var goodsDetailModel: JDOrderDetailModel?
//    var selectedShopCartModel: JDShopCartModel?
//    var preOrderModel: JDPreOrderModel?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = color("#F5F6F8")
//        self.setupUIElements()
    }
    
    func setupUIElements() {
        self.createContainerViewOne()
        self.createContainerViewTwo()
        self.createContainerViewThree()
        self.createContainerViewThreeExtra()
        self.createContainerViewFour()
        self.createContainerViewFive()
        self.createInputContainerSubView()
        self.createAddressInfoView()
        self.createTipsView()
    }
    
    func createContainerViewOne() {
        
        self.containerViewOne = UIView()
        self.contentView.addSubview(self.containerViewOne!)
        self.containerViewOne?.snp.makeConstraints({ make in
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.top.equalTo(self.contentView)
        })
        self.containerViewOne?.backgroundColor = .white
        self.containerViewOne?.makeCorner(8)
        
        let line = UIView()
        self.containerViewOne?.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalTo(self.containerViewOne!).offset(12)
            make.right.equalTo(self.containerViewOne!).offset(-12)
            make.top.equalTo(self.containerViewOne!).offset(52)
            make.height.equalTo(1)
        }
        line.backgroundColor = color("#F5F6F8")
        
        self.storeNameLabel = UILabel()
        self.containerViewOne?.addSubview(self.storeNameLabel!)
        self.storeNameLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerViewOne!).offset(12)
            make.top.equalTo(self.containerViewOne!)
            make.bottom.equalTo(line.snp.top)
        })
        self.storeNameLabel?.text = "三生有幸茶艺工艺"
        self.storeNameLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .semibold), textColor: color("#333333"))
        
        self.tagLabel = UILabel()
        self.containerViewOne?.addSubview(self.tagLabel!)
        self.tagLabel?.snp.makeConstraints({ make in
            make.right.equalTo(self.containerViewOne!)
            make.centerY.equalTo(self.storeNameLabel!)
            make.size.equalTo(CGSizeMake(80, 24))
        })
        self.tagLabel?.text = "跨境直邮"
        self.tagLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: .white, numberOfLines: 0)
        self.tagLabel?.backgroundColor = UIColor.horizontalGradientColor(leftColor: color("#FA6161"), rightColor: color("#FA8061"), colorFrame: CGRect.init(x: 0, y: 0, width: 80, height: 24))
        self.tagLabel?.makeBezierPathCorner(UIRectCorner(rawValue: UIRectCorner.topLeft.rawValue|UIRectCorner.bottomLeft.rawValue), 12)
        
        
        self.goodsInfoTableView = UITableView.init(frame: .zero, style: .plain)
        self.containerViewOne?.addSubview(self.goodsInfoTableView!)
        self.goodsInfoTableView?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerViewOne!)
            make.right.equalTo(self.containerViewOne!)
            make.top.equalTo(line.snp.bottom)
            make.height.equalTo(104*self.goodsCount)
            make.bottom.equalTo(self.containerViewOne!)
        })
        self.goodsInfoTableView?.delegate = self
        self.goodsInfoTableView?.dataSource = self
        self.goodsInfoTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "goodsCell")
        self.goodsInfoTableView?.separatorStyle = .none
        
        self.goodsInfoTableView?.reloadData()
    }
    
    func createContainerViewTwo() {
        
        self.containerViewTwo = UIView()
        self.contentView.addSubview(self.containerViewTwo!)
        self.containerViewTwo?.snp.makeConstraints({ make in
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.top.equalTo(self.containerViewOne!.snp.bottom).offset(11)
        })
        self.containerViewTwo?.backgroundColor = .white
        self.containerViewTwo?.layer.cornerRadius = 8
        
        self.guanshuiPrice = JDCellInfoCellSubView()
        self.containerViewTwo?.addSubview(self.guanshuiPrice!)
        self.guanshuiPrice?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerViewTwo!)
            make.right.equalTo(self.containerViewTwo!)
            make.top.equalTo(self.containerViewTwo!).offset(9)
            make.height.equalTo(32)
        })
   
//        self.jingneiwuliuPrice = JDCellInfoCellSubView()
//        self.containerViewTwo?.addSubview(self.jingneiwuliuPrice!)
//        self.jingneiwuliuPrice?.snp.makeConstraints({ make in
//            make.left.equalTo(self.containerViewTwo!)
//            make.right.equalTo(self.containerViewTwo!)
//            make.top.equalTo(self.guanshuiPrice!.snp.bottom)
//            make.height.equalTo(32)
//        })
        
        self.kuajingwuliuPrice = JDCellInfoCellSubView()
        self.containerViewTwo?.addSubview(self.kuajingwuliuPrice!)
        self.kuajingwuliuPrice?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerViewTwo!)
            make.right.equalTo(self.containerViewTwo!)
//            make.top.equalTo(self.jingneiwuliuPrice!.snp.bottom)
            make.top.equalTo(self.guanshuiPrice!.snp.bottom)
            make.height.equalTo(32)
            make.bottom.equalTo(self.containerViewTwo!.snp.bottom).offset(-9)
        })
        self.kuajingwuliuPrice?.titleLabel?.text = "跨境物流费"

    }
    
    func createContainerViewThree() {
        
        self.containerViewThree = UIView()
        self.contentView.addSubview(self.containerViewThree!)
        self.containerViewThree?.snp.makeConstraints({ make in
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.top.equalTo(self.containerViewTwo!.snp.bottom).offset(10)
        })
        self.containerViewThree?.backgroundColor = .white
        self.containerViewThree?.layer.cornerRadius = 8
        
        self.orderId = JDCellInfoCellSubView()
        self.containerViewThree?.addSubview(self.orderId!)
        self.orderId?.titleLabel?.text = "跨境订单号"
        
        
        if self.orderType == .pickup || self.orderType == .completed || self.orderType == .refusse {
            self.orderId?.snp.makeConstraints({ make in
                make.left.equalTo(self.containerViewThree!)
                make.right.equalTo(self.containerViewThree!)
                make.top.equalTo(self.containerViewThree!).offset(9)
                make.height.equalTo(32)
//                make.bottom.equalTo(self.containerViewThree!).offset(-9)
            })
            
            self.haiguanID = JDCellInfoCellSubView()
            self.containerViewThree?.addSubview(self.haiguanID!)
            self.haiguanID?.snp.makeConstraints({ make in
                make.left.equalTo(self.containerViewThree!)
                make.right.equalTo(self.containerViewThree!)
                make.top.equalTo(self.orderId!.snp.bottom)
                make.height.equalTo(32)
                make.bottom.equalTo(self.containerViewThree!).offset(-9)
            })
            self.haiguanID?.titleLabel?.text = "海关通关单号"
            
        } else {
            self.orderId?.snp.makeConstraints({ make in
                make.left.equalTo(self.containerViewThree!)
                make.right.equalTo(self.containerViewThree!)
                make.top.equalTo(self.containerViewThree!).offset(9)
                make.height.equalTo(32)
                make.bottom.equalTo(self.containerViewThree!).offset(-9)
            })
        }
        
        
//        self.gongyingshangNum = JDCellInfoCellSubView()
//        self.containerViewThree?.addSubview(self.gongyingshangNum!)
//        self.gongyingshangNum?.snp.makeConstraints({ make in
//            make.left.equalTo(self.containerViewThree!)
//            make.right.equalTo(self.containerViewThree!)
//            make.top.equalTo(self.orderId!.snp.bottom)
//            make.height.equalTo(32)
//            make.bottom.equalTo(self.containerViewThree!).offset(-9)
//        })
//        self.gongyingshangNum?.titleLabel?.text = "供应商编号"
    }
    
    func createContainerViewThreeExtra() {
        self.containerViewThreeExtra = UIView()
        self.contentView.addSubview(self.containerViewThreeExtra!)
        self.containerViewThreeExtra?.backgroundColor = .white
        self.containerViewThreeExtra?.layer.cornerRadius = 8
        
        if self.orderType == .waitingPay {
            self.containerViewThreeExtra?.snp.makeConstraints({ make in
                make.left.equalTo(self.contentView).offset(12)
                make.right.equalTo(self.contentView).offset(-12)
                make.top.equalTo(self.containerViewThree!.snp.bottom)
                make.height.equalTo(50)
            })
            
            self.orderCreateTime = JDCellInfoCellSubView()
            self.containerViewThreeExtra?.addSubview(self.orderCreateTime!)
            self.orderCreateTime?.snp.makeConstraints({ make in
                make.left.equalTo(self.containerViewThreeExtra!)
                make.right.equalTo(self.containerViewThreeExtra!)
//                make.top.equalTo(self.containerViewThreeExtra!).offset(9)
                make.height.equalTo(32)
                make.centerY.equalTo(self.containerViewThreeExtra!)
            })
            self.orderCreateTime?.titleLabel?.text = "下单时间"
            
            return;
        } else {
            self.containerViewThreeExtra?.snp.makeConstraints({ make in
                make.left.equalTo(self.contentView).offset(12)
                make.right.equalTo(self.contentView).offset(-12)
                make.top.equalTo(self.containerViewThree!.snp.bottom).offset(10)
            })
        }
        
        
        if self.orderType == .alreadyPay || self.orderType == .pickup || self.orderType == .completed || self.orderType == .refusse {
            self.orderCreateTime = JDCellInfoCellSubView()
            self.containerViewThreeExtra?.addSubview(self.orderCreateTime!)
            self.orderCreateTime?.snp.makeConstraints({ make in
                make.left.equalTo(self.containerViewThreeExtra!)
                make.right.equalTo(self.containerViewThreeExtra!)
                make.top.equalTo(self.containerViewThreeExtra!).offset(9)
                make.height.equalTo(32)
//                make.bottom.equalTo(self.containerViewThreeExtra!).offset(-9)
            })
            self.orderCreateTime?.titleLabel?.text = "下单时间"
            
            self.orderPayTime = JDCellInfoCellSubView()
            self.containerViewThreeExtra?.addSubview(self.orderPayTime!)
            self.orderPayTime?.snp.makeConstraints({ make in
                make.left.equalTo(self.containerViewThreeExtra!)
                make.right.equalTo(self.containerViewThreeExtra!)
                make.top.equalTo(self.orderCreateTime!.snp.bottom)
                make.height.equalTo(32)
            })
            self.orderPayTime?.titleLabel?.text = "支付时间"
            
            self.deliveryOrderTime = JDCellInfoCellSubView()
            self.containerViewThreeExtra?.addSubview(self.deliveryOrderTime!)
            self.deliveryOrderTime?.titleLabel?.text = "发货时间"
            
            if self.orderType == .pickup || self.orderType == .completed {
                
                self.deliveryOrderTime?.snp.makeConstraints({ make in
                    make.left.equalTo(self.containerViewThreeExtra!)
                    make.right.equalTo(self.containerViewThreeExtra!)
                    make.top.equalTo(self.orderPayTime!.snp.bottom)
//                    make.top.equalTo(self.orderCreateTime!.snp.bottom)
                    make.height.equalTo(32)
                })
                
                self.arriveTime = JDCellInfoCellSubView()
                self.containerViewThreeExtra?.addSubview(self.arriveTime!)
                self.arriveTime?.titleLabel?.text = "到货时间"
                
                if self.orderType == .completed {
                    self.arriveTime?.snp.makeConstraints({ make in
                        make.left.equalTo(self.containerViewThreeExtra!)
                        make.right.equalTo(self.containerViewThreeExtra!)
                        make.top.equalTo(self.deliveryOrderTime!.snp.bottom)
                        make.height.equalTo(32)
                    })
                    
                    self.finishTime = JDCellInfoCellSubView()
                    self.containerViewThreeExtra?.addSubview(self.finishTime!)
                    self.finishTime?.titleLabel?.text = "收货时间"
                    self.finishTime?.snp.makeConstraints({ make in
                        make.left.equalTo(self.containerViewThreeExtra!)
                        make.right.equalTo(self.containerViewThreeExtra!)
                        make.top.equalTo(self.arriveTime!.snp.bottom)
                        make.height.equalTo(32)
                        make.bottom.equalTo(self.containerViewThreeExtra!).offset(-9)
                    })
                    
                } else {
                    self.arriveTime?.snp.makeConstraints({ make in
                        make.left.equalTo(self.containerViewThreeExtra!)
                        make.right.equalTo(self.containerViewThreeExtra!)
                        make.top.equalTo(self.deliveryOrderTime!.snp.bottom)
                        make.height.equalTo(32)
                        make.bottom.equalTo(self.containerViewThreeExtra!).offset(-9)
                    })
                }
                
            } else {
                // 包含 已拒单
                self.deliveryOrderTime?.snp.makeConstraints({ make in
                    make.left.equalTo(self.containerViewThreeExtra!)
                    make.right.equalTo(self.containerViewThreeExtra!)
                    make.top.equalTo(self.orderPayTime!.snp.bottom)
                    make.height.equalTo(0)
                    make.bottom.equalTo(self.containerViewThreeExtra!).offset(-9)
                })
            }
            
        }
    }
    
    func createContainerViewFour() {
        
        self.containerViewFour = UIView()
        self.contentView.addSubview(self.containerViewFour!)
        self.containerViewFour?.snp.makeConstraints({ make in
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.top.equalTo(self.containerViewThreeExtra!.snp.bottom).offset(10)
            make.height.equalTo(50)
        })
        self.containerViewFour?.backgroundColor = .white
        self.containerViewFour?.layer.cornerRadius = 8
        
        self.phoneTitleLabel = UILabel()
        self.containerViewFour?.addSubview(self.phoneTitleLabel!)
        self.phoneTitleLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerViewFour!).offset(12)
            make.top.bottom.equalTo(self.containerViewFour!)
        })
        self.phoneTitleLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#8D9198"))
        self.phoneTitleLabel?.text = "客服电话：0411-87979988 占"
    
        self.callButton = UIButton(type: .custom)
        self.containerViewFour?.addSubview(self.callButton!)
        self.callButton?.snp.makeConstraints({ make in
            make.right.equalTo(self.containerViewFour!).offset(-12)
            make.centerY.equalTo(self.containerViewFour!)
            make.size.equalTo(CGSize(width: 28, height: 28))
        })
        self.callButton?.setImage(UIImage(named: "call_image")?.withRenderingMode(.alwaysOriginal), for: .normal)
        self.callButton?.addTarget(self, action: #selector(callAuthorAction), for: .touchUpInside)
    }
    
    @objc func callAuthorAction() {
        let callStr = String.init(format: "tel:%@", self.goodsDetailModel?.customerServicePhone ?? "0411-87979988")
        if let callUrl = URL(string: callStr) {
            UIApplication.shared.open(callUrl, options: [:]) { isSuccess in
            }
        }
    }
    
    
    func createContainerViewFive() {
        
        self.containerViewFive = UIView()
        self.contentView.addSubview(self.containerViewFive!)
        if self.orderType == .alreadyPay || self.orderType == .pickup || self.orderType == .completed {
            self.containerViewFive?.snp.makeConstraints({ make in
                make.left.equalTo(self.contentView).offset(12)
                make.right.equalTo(self.contentView).offset(-12)
                make.top.equalTo(self.containerViewFour!.snp.bottom).offset(10)
            })
        } else {
            // 包含 已拒单
            self.containerViewFive?.snp.makeConstraints({ make in
                make.left.equalTo(self.contentView).offset(12)
                make.right.equalTo(self.contentView).offset(-12)
                make.top.equalTo(self.containerViewFour!.snp.bottom).offset(10)
                make.height.equalTo(0)
            })
        }
        self.containerViewFive?.backgroundColor = .white
        self.containerViewFive?.layer.cornerRadius = 8
        self.containerViewFive?.clipsToBounds = true
        
        self.expressTitle = UILabel()
        self.containerViewFive?.addSubview(self.expressTitle!)
        self.expressTitle?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerViewFive!).offset(12)
            make.top.equalTo(self.containerViewFive!).offset(15)
            make.height.equalTo(20)
        })
        self.expressTitle?.text = "运单号"
        self.expressTitle?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#8D9198"), numberOfLines: 1)

        self.expressId = UILabel()
        self.containerViewFive?.addSubview(self.expressId!)
        self.expressId?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerViewFive!).offset(12)
            make.top.equalTo(self.expressTitle!.snp.bottom).offset(8)
            make.height.equalTo(20)
            make.bottom.equalTo(self.containerViewFive!).offset(-15)
        })
        self.expressId?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        self.expressId?.text = "12312312"
        
//        var expressDeatilButton: UIButton?
        self.expressDeatilButton = UIButton(type: .custom)
        self.containerViewFive?.addSubview(self.expressDeatilButton!)
        self.expressDeatilButton?.snp.makeConstraints({ make in
            make.right.equalTo(self.containerViewFive!).offset(-16)
            make.centerY.equalTo(self.expressId!)
            make.height.equalTo(20)
        })
        self.expressDeatilButton?.setTitle("查看", for: .normal)
        self.expressDeatilButton?.buttonConfig(normalTitle: "查看",normalTitleColor: color("#FA6161"), titleFont: UIFont.systemFont(ofSize: 14, weight: .regular))
        self.expressDeatilButton?.addTarget(self, action: #selector(expressDeatilAction), for: .touchUpInside)
    }
     
    func createInputContainerSubView() {
        
        self.inputContainerViewTitle = UILabel()
        self.contentView.addSubview(self.inputContainerViewTitle!)
        
        
        if self.orderType == .alreadyPay || self.orderType == .pickup || self.orderType == .completed {
            self.inputContainerViewTitle?.snp.makeConstraints({ make in
                make.left.equalTo(self.contentView).offset(24)
                make.top.equalTo(self.containerViewFive!.snp.bottom).offset(22)
                make.height.equalTo(22)
            })
        } else {
            self.inputContainerViewTitle?.snp.makeConstraints({ make in
                make.left.equalTo(self.contentView).offset(24)
                make.top.equalTo(self.containerViewFour!.snp.bottom).offset(22)
                make.height.equalTo(22)
            })
        }
        
        self.inputContainerViewTitle?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .semibold), textColor: color("#333333"))
        self.inputContainerViewTitle?.text = "购买人信息"
        
        let userInfoInputImgV = UIImageView()
        self.contentView.addSubview(userInfoInputImgV)
        userInfoInputImgV.snp.makeConstraints { make in
            make.left.equalTo(self.inputContainerViewTitle!.snp.right).offset(6)
            make.centerY.equalTo(self.inputContainerViewTitle!)
            make.size.equalTo(CGSizeMake(16, 16))
        }
        userInfoInputImgV.image = UIImage(named: "userInfo_image")?.withRenderingMode(.alwaysOriginal)
        
        self.inputContainerView = UIView()
        self.contentView.addSubview(self.inputContainerView!)
        self.inputContainerView?.snp.makeConstraints({ make in
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.top.equalTo(self.inputContainerViewTitle!.snp.bottom).offset(14)
        })
        self.inputContainerView?.layer.cornerRadius = 8
        self.inputContainerView?.backgroundColor = .white
        
        let topLine = UIView()
        self.inputContainerView?.addSubview(topLine)
        topLine.snp.makeConstraints { make in
            make.left.equalTo(self.inputContainerView!).offset(12)
            make.right.equalTo(self.inputContainerView!).offset(-12)
            make.top.equalTo(self.inputContainerView!).offset(55)
            make.height.equalTo(0.5)
        }
        topLine.backgroundColor = color("#EDEEF1")
        
        
        let nameTitle = UILabel()
        self.inputContainerView?.addSubview(nameTitle)
        nameTitle.snp.makeConstraints { make in
            make.top.equalTo(self.inputContainerView!)
            make.bottom.equalTo(topLine.snp.top)
            make.left.equalTo(self.inputContainerView!).offset(12)
        }
        nameTitle.text = "真实姓名 "
        nameTitle.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"))
        nameTitle.appendAttributedString(appenStr: "*", attrs: [.foregroundColor: color("#FA6161"),.font: UIFont.systemFont(ofSize: 14, weight: .regular)])
        
        self.shopperNameInputView = UITextField()
        self.inputContainerView?.addSubview(self.shopperNameInputView!)
        self.shopperNameInputView?.snp.makeConstraints({ make in
            make.left.equalTo(self.inputContainerView!).offset(90)
//            make.left.equalTo(nameTitle.snp.right).offset(12)
            make.right.equalTo(self.inputContainerView!).offset(-12)
            make.top.equalTo(self.inputContainerView!)
            make.bottom.equalTo(topLine.snp.top)
        })
        self.shopperNameInputView?.textColor = color("#333333")
        self.shopperNameInputView?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        self.shopperNameInputView?.placeholder = "购买人真实姓名"
        self.shopperNameInputView?.isEnabled = false
        self.shopperNameInputView?.isUserInteractionEnabled = false
        
        let middleLine = UIView()
        self.inputContainerView?.addSubview(middleLine)
        middleLine.snp.makeConstraints { make in
            make.left.equalTo(self.inputContainerView!).offset(12)
            make.right.equalTo(self.inputContainerView!).offset(-12)
            make.top.equalTo(topLine.snp.bottom).offset(60)
            make.height.equalTo(0.5)
            make.bottom.equalTo(self.inputContainerView!)
        }
        middleLine.backgroundColor = color("#EDEEF1")
        middleLine.isHidden = true
        
        let idCardTitle = UILabel()
        self.inputContainerView?.addSubview(idCardTitle)
        idCardTitle.snp.makeConstraints { make in
            make.left.equalTo(self.inputContainerView!).offset(12)
            make.top.equalTo(topLine.snp.bottom)
            make.bottom.equalTo(middleLine.snp.top)
        }
        idCardTitle.text = "身份证号 "
        idCardTitle.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"))
        idCardTitle.appendAttributedString(appenStr: "*", attrs: [.foregroundColor: color("#FA6161"),.font: UIFont.systemFont(ofSize: 14, weight: .regular)])
        idCardTitle.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        
        self.shopperIDNumberInputView = UITextField()
        self.inputContainerView?.addSubview(self.shopperIDNumberInputView!)
        self.shopperIDNumberInputView?.snp.makeConstraints({ make in
//            make.left.equalTo(self.inputContainerView!).offset(12)
            make.left.equalTo(self.shopperNameInputView!)
            make.top.equalTo(topLine.snp.bottom)
            make.bottom.equalTo(middleLine.snp.top)
            make.right.equalTo(self.inputContainerView!).offset(-12)
        })
        self.shopperIDNumberInputView?.placeholder = "购买人身份证号"
        self.shopperIDNumberInputView?.textColor = color("#333333")
        self.shopperIDNumberInputView?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        self.shopperIDNumberInputView?.isEnabled = false
        self.shopperIDNumberInputView?.isUserInteractionEnabled = false
        
//        self.shopperIDNumberLabel = UILabel()
//        self.inputContainerView?.addSubview(self.shopperIDNumberLabel!)
//        self.shopperIDNumberLabel?.snp.makeConstraints({ make in
//            make.right.equalTo(self.inputContainerView!).offset(-12)
//            make.centerY.equalTo(self.shopperIDNumberInputView!)
//        })
//        self.shopperIDNumberLabel?.labelConfig(textAlignment: .right, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"), numberOfLines: 0)
//        self.shopperIDNumberLabel?.text = "210802199010101010"
//        self.shopperIDNumberLabel?.backgroundColor = .
        return;
//        self.shopperMessageInputView = UITextView()
//        self.inputContainerView?.addSubview(self.shopperMessageInputView!)
//        self.shopperMessageInputView?.snp.makeConstraints({ make in
//            make.left.equalTo(self.inputContainerView!).offset(12)
//            make.right.equalTo(self.inputContainerView!).offset(-12)
//            make.top.equalTo(middleLine.snp.bottom)//.offset(20)
//            make.height.equalTo(70)
//            make.bottom.equalTo(self.inputContainerView!).offset(-15)
//        })
//        self.shopperMessageInputView?.placeholder = "用户留言"
//        self.shopperMessageInputView?.textColor = color("#333333")
//        self.shopperMessageInputView?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
//        self.shopperMessageInputView?.isUserInteractionEnabled = false
//        self.shopperMessageInputView?.isEditable = false
    }
    
    func createAddressInfoView() {
        
        self.addressInfoViewTitle = UILabel()
        self.contentView.addSubview(self.addressInfoViewTitle!)
        self.addressInfoViewTitle?.snp.makeConstraints({ make in
            make.left.equalTo(self.contentView).offset(24)
            make.top.equalTo(self.inputContainerView!.snp.bottom).offset(22)
            make.height.equalTo(22)
        })
        self.addressInfoViewTitle?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .semibold), textColor: color("#333333"))
        self.addressInfoViewTitle?.text = "收货信息"
        
        self.addressInfoImgV = UIImageView()
        self.contentView.addSubview(self.addressInfoImgV!)
        self.addressInfoImgV?.snp.makeConstraints({ make in
            make.centerY.equalTo(self.addressInfoViewTitle!)
            make.left.equalTo(self.addressInfoViewTitle!.snp.right).offset(6)
            make.size.equalTo(CGSize(width: 14, height: 14))
        })
        self.addressInfoImgV?.image = UIImage(named: "location")?.withRenderingMode(.alwaysOriginal)
        
        self.addressInfoContainerView = UIView()
        self.contentView.addSubview(self.addressInfoContainerView!)
        self.addressInfoContainerView?.snp.makeConstraints({ make in
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.top.equalTo(self.addressInfoViewTitle!.snp.bottom).offset(14)
        })
        self.addressInfoContainerView?.backgroundColor = .white
        self.addressInfoContainerView?.layer.cornerRadius = 8
        
        self.shopperNameLabel = UILabel()
        self.addressInfoContainerView?.addSubview(self.shopperNameLabel!)
        self.shopperNameLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.addressInfoContainerView!).offset(12)
            make.top.equalTo(self.addressInfoContainerView!).offset(16)
            make.height.equalTo(25)
        })
        self.shopperNameLabel?.text = "罗先生"
        self.shopperNameLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 18, weight: .semibold), textColor: color("#333333"))
        
        self.shopperTelLabel = UILabel()
        self.addressInfoContainerView?.addSubview(self.shopperTelLabel!)
        self.shopperTelLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.addressInfoContainerView!).offset(12)
            make.top.equalTo(self.shopperNameLabel!.snp.bottom).offset(5)
            make.height.equalTo(20)
        })
        self.shopperTelLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"))
        self.shopperTelLabel?.text = "13644930209"
        
        self.shopperAddressLabel = UILabel()
        self.addressInfoContainerView?.addSubview(self.shopperAddressLabel!)
        self.shopperAddressLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.addressInfoContainerView!).offset(12)
            make.top.equalTo(self.shopperTelLabel!.snp.bottom).offset(5)
            make.height.equalTo(20)
            make.bottom.equalTo(self.addressInfoContainerView!).offset(-18)
        })
        self.shopperAddressLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"))
        self.shopperAddressLabel?.text = "辽宁省大连市甘井子区高云路136号2-1"
    }
    
    func createTipsView() {
        
        self.tipsViewTitle = UILabel()
        self.contentView.addSubview(self.tipsViewTitle!)
        self.tipsViewTitle?.snp.makeConstraints({ make in
            make.left.equalTo(self.contentView).offset(24)
            make.top.equalTo(self.addressInfoContainerView!.snp.bottom).offset(22)
            make.height.equalTo(22)
        })
        self.tipsViewTitle?.text = "平台提示"
        self.tipsViewTitle?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .semibold), textColor: color("#333333"))
        
        self.hintImgV = UIImageView()
        self.contentView.addSubview(self.hintImgV!)
        self.hintImgV?.snp.makeConstraints({ make in
            make.left.equalTo(self.tipsViewTitle!.snp.right).offset(6)
            make.centerY.equalTo(self.tipsViewTitle!)
            make.size.equalTo(CGSize(width: 11, height: 14))
        })
        self.hintImgV?.image = UIImage(named: "hint")?.withRenderingMode(.alwaysOriginal)
        
        self.tipsContainerView = UIView()
        self.contentView.addSubview(self.tipsContainerView!)
        self.tipsContainerView?.snp.makeConstraints({ make in
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.top.equalTo(self.tipsViewTitle!.snp.bottom).offset(14)
            make.bottom.equalTo(self.contentView).offset(-25)
        })
        self.tipsContainerView?.layer.cornerRadius = 8
        self.tipsContainerView?.backgroundColor = color("#FFFDFA")
        
        self.tipsLabel = UILabel()
        self.tipsContainerView?.addSubview(self.tipsLabel!)
        self.tipsLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.tipsContainerView!).offset(12)
            make.right.equalTo(self.tipsContainerView!).offset(-12)
            make.top.equalTo(self.tipsContainerView!).offset(12)
            make.bottom.equalTo(self.tipsContainerView!).offset(-12)
        })
        self.tipsLabel?.text = "· 您所购商品是平台依照您的订单申办进口的，所以不能为您提供退货换货响应且可能被拒单，您下单后力争20天内送达；\n· 国内配送费由用户承担，待货品送达后，由用户向承运公司自行支付。\n· 购买跨境商品，平台接单后不接受取消订单或商品退换诉求。"
        
        self.tipsLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"), numberOfLines: 0)
        self.tipsLabel?.addTextLineSpace(spaceHeight: 8)
    }
    
    func reloadData() {
        
        for subV in self.contentView.subviews {
            subV.removeFromSuperview()
        }
        
        self.goodsCount = self.goodsDetailModel?.goodsList?.count ?? 0
        printLog("商品数量:\(self.goodsCount)")
        setupUIElements()
        
        self.storeNameLabel?.text = self.shopName
        self.shopperNameInputView?.text = self.goodsDetailModel?.buyerName
        self.shopperIDNumberInputView?.text = self.goodsDetailModel?.buyerIdCardNumber
        
        self.guanshuiPrice?.descriptionLabel?.text = String.init(format: "￥%.2f", JSON(self.goodsDetailModel?.tariff as Any).doubleValue/1000.0)
        self.kuajingwuliuPrice?.descriptionLabel?.text = String.init(format: "￥%.2f", JSON(self.goodsDetailModel?.freight as Any).doubleValue/1000.0)
        
        self.orderId?.descriptionLabel?.text = self.goodsDetailModel?.orderNo
        
        self.shopperNameLabel?.text = self.goodsDetailModel?.buyerName
        self.shopperIDNumberLabel?.text = self.goodsDetailModel?.buyerIdCardNumber
        self.shopperMessageInputView?.text = self.goodsDetailModel?.userMessage
        
        
        self.phoneTitleLabel?.text = String.init(format: "客服电话：%@", self.goodsDetailModel?.customerServicePhone ?? "0411-11011011")
        
      
        self.shopperNameLabel?.text = self.goodsDetailModel?.receiverName
        self.shopperTelLabel?.text = String.init(format: "%@ · %@", self.goodsDetailModel?.mobile ?? "", self.goodsDetailModel?.postal ?? "")
        self.shopperAddressLabel?.text = String.init(format: "%@%@%@%@", self.goodsDetailModel?.province ?? "",self.goodsDetailModel?.city ?? "",self.goodsDetailModel?.district ?? "",self.goodsDetailModel?.address ?? "" )
        
        if self.orderType == .waitingPay {
            self.orderCreateTime?.descriptionLabel?.text = self.goodsDetailModel?.orderTime
        }

        if self.orderType == .alreadyPay || self.orderType == .pickup || self.orderType == .completed || self.orderType == .refusse {
            self.orderPayTime?.descriptionLabel?.text = self.goodsDetailModel?.payTime
            self.orderCreateTime?.descriptionLabel?.text = self.goodsDetailModel?.orderTime
            self.deliveryOrderTime?.descriptionLabel?.text = self.goodsDetailModel?.deliveryTime
            
            self.expressId?.text = self.goodsDetailModel?.deliveryNo
            
            if self.orderType == .pickup || self.orderType == .completed {
                self.haiguanID?.descriptionLabel?.text = "海关单号，暂无字段"
                self.arriveTime?.descriptionLabel?.text = String.init(format: "%@", self.goodsDetailModel?.arrivalTime ?? "--")
                
                if self.orderType == .completed {
                    self.finishTime?.descriptionLabel?.text = self.goodsDetailModel?.finishTime
                }
            } else if self.orderType == .refusse {
                self.haiguanID?.descriptionLabel?.text = "海关单号，暂无字段"
                self.deliveryOrderTime?.titleLabel?.text = "发货时间"
                self.deliveryOrderTime?.descriptionLabel?.text = self.goodsDetailModel?.deliveryTime
            } else if self.orderType == .alreadyPay {
                self.expressId?.text = ((self.goodsDetailModel?.deliveryNo?.count ?? 0) > 0) ? self.goodsDetailModel?.deliveryNo : "待商家填写运单号"
                self.expressId?.textColor = color("#8D9198")
            }
            
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @objc func expressDeatilAction() {
        
        if let deliveryNo = self.goodsDetailModel?.deliveryNo, deliveryNo.count > 0 {
            self.logisticsInfoCallBack?(deliveryNo)
        } else {
            JDTools.getKeyWindows()?.rootViewController?.view.toast(message: "暂无订单号，请等待商家填写运单号")
        }
    }
    
}



extension JDOrderDetailCell: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.goodsCount
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "goodsCell")
        
        let goodsModel = self.goodsDetailModel?.goodsList?[indexPath.row]
        
        let goodsInfoView = JDOrderGoodsView()
        cell?.contentView.addSubview(goodsInfoView)
        goodsInfoView.snp.makeConstraints { make in
            make.height.equalTo(104)
            make.top.equalTo(cell!.contentView)
            make.left.equalTo(cell!.contentView)
            make.right.equalTo(cell!.contentView)
        }
        goodsInfoView.goodsName.text = goodsModel?.goodsName
        goodsInfoView.goodsSpec.text = String.init(format: "规格：%@", goodsModel?.specificationName ?? "")
        
        
//        goodsSpecificationNameList
        
        goodsInfoView.goodsNum.text = String.init(format: "数量：%@", goodsModel?.goodsCount ?? 0)
        goodsInfoView.goodsAddress.text = String.init(format: "产地：%@", goodsModel?.origin ?? "")
        
       
        goodsInfoView.goodsPrice.text = String.init(format: "￥%@", goodsModel?.goodsPriceText ?? "0.00")
        goodsInfoView.goodsPrice.priceString(UIFont.systemFont(ofSize: 12, weight: .regular), UIFont.systemFont(ofSize: 16, weight: .semibold))
        
        if goodsModel?.goodsMainImageUrl?.hasPrefix("http") ?? false  {
            goodsInfoView.goodsImgV.kf.setImage(with: URL(string: goodsModel?.goodsMainImageUrl ?? ""),placeholder: UIImage(named: "loading_placeholder"))
        } else {
            goodsInfoView.goodsImgV.downloadImage(goodsModel?.goodsMainImageUrl ?? "")
        }
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 104
    }
    
}

